package com.team.arms.yuding.controller;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.yuding.domain.VolunteerSchedule;
import com.team.arms.yuding.service.VolunteerScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjh
 * @since 2022-04-27
 */
@Api( tags = "志愿时间")
@RestController
@RequestMapping("/reservesoldiers/schedule")
public class VolunteerScheduleController extends BaseController {

    @Autowired
    private VolunteerScheduleService volunteerScheduleService;

    /**
     * 查询志愿时间信息
     * @return
     */
    @ApiOperation("查询志愿时间信息")
    @GetMapping("/selectschedule")
    public AjaxResult selectSchedule(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                     @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<VolunteerSchedule> list = volunteerScheduleService.list();
        PageInfo<VolunteerSchedule> pageInfo = new PageInfo<VolunteerSchedule>(list);
        PageVO<VolunteerSchedule> volunteerSchedulePageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, volunteerSchedulePageVO);
        volunteerSchedulePageVO.setResult(pageInfo.getList());
        volunteerSchedulePageVO.setPage(pageNum);
        return AjaxResult.success(volunteerSchedulePageVO);
    }

    @ApiOperation("根据id查询志愿时间信息")
    @GetMapping("/selectById")
    public AjaxResult selectById(@RequestParam(value = "volunteer_schedule_id")String id){
        return AjaxResult.success(volunteerScheduleService.getById(id));
    }

    /**
     * 修改志愿时间信息
     * @param volunteerSchedule
     * @return
     */

    @ApiOperation("修改志愿时间信息")
    @PutMapping("/updateschedule")
    public AjaxResult updateSchedule(@RequestBody VolunteerSchedule volunteerSchedule){
        return toAjax((volunteerScheduleService.updateVolunteerSchedule(volunteerSchedule)));
    }

    /**
     * 新增志愿时间信息
     * @param volunteerSchedule
     * @return
     */
    @ApiOperation("新增志愿时间信息")
    @PostMapping("/installschedule")
    public AjaxResult installSchedule(@RequestBody VolunteerSchedule volunteerSchedule){
        return toAjax(volunteerScheduleService.insertAll(volunteerSchedule));
    }

    /**
     * 批量删除志愿时间信息
     * @param ids
     * @return
     */
    @ApiOperation("批量删除志愿时间信息")
    @DeleteMapping("/deleteschedule")
    public AjaxResult deleteSchedule(@RequestBody String[] ids){
        ArrayList<String> list = new ArrayList<String>();
        for(String id : ids){
            list.add(id);
        }
        return toAjax(volunteerScheduleService.removeBatchByIds(list));
    }

    /**
     * 删除志愿时间信息
     * @param id
     * @return
     */
    @ApiOperation("删除志愿时间信息")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam("volunteer_info_id") String id){
        return toAjax(volunteerScheduleService.removeById(id));
    }
    /**
     * 导出志愿时间信息
     */
    @ApiOperation("导出志愿时间信息")
    @Log(title = "志愿时间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<VolunteerSchedule> volunteerSchedules = volunteerScheduleService.list();//调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), VolunteerSchedule.class).sheet("志愿时间信息")
                    .doWrite(volunteerSchedules);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

